home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / include / X11 / Xthreads.h < prev    next >
C/C++ Source or Header  |  2006-04-12  |  12KB  |  322 lines

  1. /*
  2.  * $Xorg: Xthreads.h,v 1.5 2001/02/09 02:03:23 xorgcvs Exp $
  3.  *
  4.  * 
  5. Copyright 1993, 1998  The Open Group
  6.  
  7. Permission to use, copy, modify, distribute, and sell this software and its
  8. documentation for any purpose is hereby granted without fee, provided that
  9. the above copyright notice appear in all copies and that both that
  10. copyright notice and this permission notice appear in supporting
  11. documentation.
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  19. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  20. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22.  
  23. Except as contained in this notice, the name of The Open Group shall not be
  24. used in advertising or otherwise to promote the sale, use or other dealings
  25. in this Software without prior written authorization from The Open Group.
  26.  * *
  27.  */
  28. /* $XFree86: Xthreads.h,v 3.10 2001/12/14 19:53:26 dawes Exp $ */
  29.  
  30. #ifndef _XTHREADS_H_
  31. #define _XTHREADS_H_
  32.  
  33. /* Redefine these to XtMalloc/XtFree or whatever you want before including
  34.  * this header file.
  35.  */
  36. #ifndef xmalloc
  37. #define xmalloc malloc
  38. #endif
  39. #ifndef xfree
  40. #define xfree free
  41. #endif
  42.  
  43. #ifdef CTHREADS
  44. #include <cthreads.h>
  45. typedef cthread_t xthread_t;
  46. typedef struct condition xcondition_rec;
  47. typedef struct mutex xmutex_rec;
  48. #define xthread_init() cthread_init()
  49. #define xthread_self cthread_self
  50. #define xthread_fork(func,closure) cthread_fork(func,closure)
  51. #define xthread_yield() cthread_yield()
  52. #define xthread_exit(v) cthread_exit(v)
  53. #define xthread_set_name(t,str) cthread_set_name(t,str)
  54. #define xmutex_init(m) mutex_init(m)
  55. #define xmutex_clear(m) mutex_clear(m)
  56. #define xmutex_lock(m) mutex_lock(m)
  57. #define xmutex_unlock(m) mutex_unlock(m)
  58. #define xmutex_set_name(m,str) mutex_set_name(m,str)
  59. #define xcondition_init(cv) condition_init(cv)
  60. #define xcondition_clear(cv) condition_clear(cv)
  61. #define xcondition_wait(cv,m) condition_wait(cv,m)
  62. #define xcondition_signal(cv) condition_signal(cv)
  63. #define xcondition_broadcast(cv) condition_broadcast(cv)
  64. #define xcondition_set_name(cv,str) condition_set_name(cv,str)
  65. #else /* !CTHREADS */
  66. #if defined(SVR4) && !defined(__sgi) && !defined(_SEQUENT_)
  67. #include <thread.h>
  68. #include <synch.h>
  69. typedef thread_t xthread_t;
  70. typedef thread_key_t xthread_key_t;
  71. typedef cond_t xcondition_rec;
  72. typedef mutex_t xmutex_rec;
  73. #define xthread_self thr_self
  74. #define xthread_fork(func,closure) thr_create(NULL,0,func,closure,THR_NEW_LWP|THR_DETACHED,NULL)
  75. #define xthread_yield() thr_yield()
  76. #define xthread_exit(v) thr_exit(v)
  77. #define xthread_key_create(kp,d) thr_keycreate(kp,d)
  78. #ifdef sun
  79. #define xthread_key_delete(k) 0
  80. #else
  81. #define xthread_key_delete(k) thr_keydelete(k)
  82. #endif
  83. #define xthread_set_specific(k,v) thr_setspecific(k,v)
  84. #define xthread_get_specific(k,vp) thr_getspecific(k,vp)
  85. #define xmutex_init(m) mutex_init(m,USYNC_THREAD,0)
  86. #define xmutex_clear(m) mutex_destroy(m)
  87. #define xmutex_lock(m) mutex_lock(m)
  88. #define xmutex_unlock(m) mutex_unlock(m)
  89. #define xcondition_init(cv) cond_init(cv,USYNC_THREAD,0)
  90. #define xcondition_clear(cv) cond_destroy(cv)
  91. #define xcondition_wait(cv,m) cond_wait(cv,m)
  92. #define xcondition_signal(cv) cond_signal(cv)
  93. #define xcondition_broadcast(cv) cond_broadcast(cv)
  94. #else /* !SVR4 */
  95. #ifdef WIN32
  96. #define BOOL wBOOL
  97. #ifdef Status
  98. #undef Status
  99. #define Status wStatus
  100. #endif
  101. #include <windows.h>
  102. #ifdef Status
  103. #undef Status
  104. #define Status int
  105. #endif
  106. #undef BOOL
  107. typedef DWORD xthread_t;
  108. typedef DWORD xthread_key_t;
  109. struct _xthread_waiter {
  110.     HANDLE sem;
  111.     struct _xthread_waiter *next;
  112. };
  113. typedef struct {
  114.     CRITICAL_SECTION cs;
  115.     struct _xthread_waiter *waiters;
  116. } xcondition_rec;
  117. typedef CRITICAL_SECTION xmutex_rec;
  118. #define xthread_init() _Xthread_init()
  119. #define xthread_self GetCurrentThreadId
  120. #define xthread_fork(func,closure) { \
  121.     DWORD _tmptid; \
  122.     CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, (LPVOID)closure, 0, \
  123.          &_tmptid); \
  124. }
  125. #define xthread_yield() Sleep(0)
  126. #define xthread_exit(v) ExitThread((DWORD)(v))
  127. #define xthread_key_create(kp,d) *(kp) = TlsAlloc()
  128. #define xthread_key_delete(k) TlsFree(k)
  129. #define xthread_set_specific(k,v) TlsSetValue(k,v)
  130. #define xthread_get_specific(k,vp) TlsGetValue(k)
  131. #define xmutex_init(m) InitializeCriticalSection(m)
  132. #define xmutex_clear(m) DeleteCriticalSection(m)
  133. #define _XMUTEX_NESTS
  134. #define xmutex_lock(m) EnterCriticalSection(m)
  135. #define xmutex_unlock(m) LeaveCriticalSection(m)
  136. #define xcondition_init(cv) { \
  137.     InitializeCriticalSection(&(cv)->cs); \
  138.     (cv)->waiters = NULL; \
  139. }
  140. #define xcondition_clear(cv) DeleteCriticalSection(&(cv)->cs)
  141. extern struct _xthread_waiter *_Xthread_waiter();
  142. #define xcondition_wait(cv,m) { \
  143.     struct _xthread_waiter *_tmpthr = _Xthread_waiter(); \
  144.     EnterCriticalSection(&(cv)->cs); \
  145.     _tmpthr->next = (cv)->waiters; \
  146.     (cv)->waiters = _tmpthr; \
  147.     LeaveCriticalSection(&(cv)->cs); \
  148.     LeaveCriticalSection(m); \
  149.     WaitForSingleObject(_tmpthr->sem, INFINITE); \
  150.     EnterCriticalSection(m); \
  151. }
  152. #define xcondition_signal(cv) { \
  153.     EnterCriticalSection(&(cv)->cs); \
  154.     if ((cv)->waiters) { \
  155.         ReleaseSemaphore((cv)->waiters->sem, 1, NULL); \
  156.     (cv)->waiters = (cv)->waiters->next; \
  157.     } \
  158.     LeaveCriticalSection(&(cv)->cs); \
  159. }
  160. #define xcondition_broadcast(cv) { \
  161.     struct _xthread_waiter *_tmpthr; \
  162.     EnterCriticalSection(&(cv)->cs); \
  163.     for (_tmpthr = (cv)->waiters; _tmpthr; _tmpthr = _tmpthr->next) \
  164.     ReleaseSemaphore(_tmpthr->sem, 1, NULL); \
  165.     (cv)->waiters = NULL; \
  166.     LeaveCriticalSection(&(cv)->cs); \
  167. }
  168. #else /* !WIN32 */
  169. #ifdef USE_TIS_SUPPORT
  170. /*
  171.  * TIS support is intended for thread safe libraries.
  172.  * This should not be used for general client programming.
  173.  */
  174. #include <tis.h>
  175. typedef pthread_t xthread_t;
  176. typedef pthread_key_t xthread_key_t;
  177. typedef pthread_cond_t xcondition_rec;
  178. typedef pthread_mutex_t xmutex_rec;
  179. #define xthread_self tis_self
  180. #define xthread_fork(func,closure) { pthread_t _tmpxthr; \
  181.         pthread_create(&_tmpxthr,NULL,func,closure); }
  182. #define xthread_yield() pthread_yield_np()
  183. #define xthread_exit(v) pthread_exit(v)
  184. #define xthread_key_create(kp,d) tis_key_create(kp,d)
  185. #define xthread_key_delete(k) tis_key_delete(k)
  186. #define xthread_set_specific(k,v) tis_setspecific(k,v)
  187. #define xthread_get_specific(k,vp) *(vp) = tis_getspecific(k)
  188. #define XMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
  189. #define xmutex_init(m) tis_mutex_init(m)
  190. #define xmutex_clear(m) tis_mutex_destroy(m)
  191. #define xmutex_lock(m) tis_mutex_lock(m)
  192. #define xmutex_unlock(m) tis_mutex_unlock(m)
  193. #define xcondition_init(c) tis_cond_init(c)
  194. #define xcondition_clear(c) tis_cond_destroy(c)
  195. #define xcondition_wait(c,m) tis_cond_wait(c,m)
  196. #define xcondition_signal(c) tis_cond_signal(c)
  197. #define xcondition_broadcast(c) tis_cond_broadcast(c)
  198. #else
  199. #ifdef USE_NBSD_THREADLIB
  200. /*
  201.  * NetBSD threadlib support is intended for thread safe libraries.
  202.  * This should not be used for general client programming.
  203.  */
  204. #include <threadlib.h>
  205. typedef thr_t xthread_t;
  206. typedef thread_key_t xthread_key_t;
  207. typedef cond_t xcondition_rec;
  208. typedef mutex_t xmutex_rec;
  209. #define xthread_self thr_self
  210. #define xthread_fork(func,closure) { thr_t _tmpxthr; \
  211.     /* XXX Create it detached?  --thorpej */ \
  212.     thr_create(&_tmpxthr,NULL,func,closure); }
  213. #define xthread_yield() thr_yield()
  214. #define xthread_exit(v) thr_exit(v)
  215. #define xthread_key_create(kp,d) thr_keycreate(kp,d)
  216. #define xthread_key_delete(k) thr_keydelete(k)
  217. #define xthread_set_specific(k,v) thr_setspecific(k,v)
  218. #define xthread_get_specific(k,vp) *(vp) = thr_getspecific(k)
  219. #define XMUTEX_INITIALIZER MUTEX_INITIALIZER
  220. #define xmutex_init(m) mutex_init(m, 0)
  221. #define xmutex_clear(m) mutex_destroy(m)
  222. #define xmutex_lock(m) mutex_lock(m)
  223. #define xmutex_unlock(m) mutex_unlock(m)
  224. #define xcondition_init(c) cond_init(c, 0, 0)
  225. #define xcondition_clear(c) cond_destroy(c)
  226. #define xcondition_wait(c,m) cond_wait(c,m)
  227. #define xcondition_signal(c) cond_signal(c)
  228. #define xcondition_broadcast(c) cond_broadcast(c)
  229. #else
  230. #include <pthread.h>
  231. typedef pthread_t xthread_t;
  232. typedef pthread_key_t xthread_key_t;
  233. typedef pthread_cond_t xcondition_rec;
  234. typedef pthread_mutex_t xmutex_rec;
  235. #define xthread_self pthread_self
  236. #define xthread_yield() pthread_yield()
  237. #define xthread_exit(v) pthread_exit(v)
  238. #define xthread_set_specific(k,v) pthread_setspecific(k,v)
  239. #define xmutex_clear(m) pthread_mutex_destroy(m)
  240. #define xmutex_lock(m) pthread_mutex_lock(m)
  241. #define xmutex_unlock(m) pthread_mutex_unlock(m)
  242. #ifndef XPRE_STANDARD_API
  243. #define xthread_key_create(kp,d) pthread_key_create(kp,d)
  244. #define xthread_key_delete(k) pthread_key_delete(k)
  245. #define xthread_get_specific(k,vp) *(vp) = pthread_getspecific(k)
  246. #define xthread_fork(func,closure) { pthread_t _tmpxthr; \
  247.     pthread_create(&_tmpxthr,NULL,func,closure); }
  248. #define XMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
  249. #define xmutex_init(m) pthread_mutex_init(m, NULL)
  250. #define xcondition_init(c) pthread_cond_init(c, NULL)
  251. #else /* XPRE_STANDARD_API */
  252. #define xthread_key_create(kp,d) pthread_keycreate(kp,d)
  253. #define xthread_key_delete(k) 0
  254. #define xthread_get_specific(k,vp) pthread_getspecific(k,vp)
  255. #define xthread_fork(func,closure) { pthread_t _tmpxthr; \
  256.     pthread_create(&_tmpxthr,pthread_attr_default,func,closure); }
  257. #define xmutex_init(m) pthread_mutex_init(m, pthread_mutexattr_default)
  258. #define xcondition_init(c) pthread_cond_init(c, pthread_condattr_default)
  259. #endif /* XPRE_STANDARD_API */
  260. #define xcondition_clear(c) pthread_cond_destroy(c)
  261. #define xcondition_wait(c,m) pthread_cond_wait(c,m)
  262. #define xcondition_signal(c) pthread_cond_signal(c)
  263. #define xcondition_broadcast(c) pthread_cond_broadcast(c)
  264. #if defined(_DECTHREADS_)
  265. static xthread_t _X_no_thread_id;
  266. #define xthread_have_id(id) !pthread_equal(id, _X_no_thread_id)
  267. #define xthread_clear_id(id) id = _X_no_thread_id
  268. #define xthread_equal(id1,id2) pthread_equal(id1, id2)
  269. #endif /* _DECTHREADS_ */
  270. #if defined(__linux__)
  271. #define xthread_have_id(id) !pthread_equal(id, 0)
  272. #define xthread_clear_id(id) id = 0
  273. #define xthread_equal(id1,id2) pthread_equal(id1, id2)
  274. #endif /* linux */
  275. #if defined(_CMA_VENDOR_) && defined(_CMA__IBM) && (_CMA_VENDOR_ == _CMA__IBM)
  276. #ifdef DEBUG            /* too much of a hack to enable normally */
  277. /* see also cma__obj_set_name() */
  278. #define xmutex_set_name(m,str) ((char**)(m)->field1)[5] = (str)
  279. #define xcondition_set_name(cv,str) ((char**)(cv)->field1)[5] = (str)
  280. #endif /* DEBUG */
  281. #endif /* _CMA_VENDOR_ == _CMA__IBM */
  282. #endif /* USE_NBSD_THREADLIB */
  283. #endif /* USE_TIS_SUPPORT */
  284. #endif /* WIN32 */
  285. #endif /* SVR4 */
  286. #endif /* CTHREADS */
  287. typedef xcondition_rec *xcondition_t;
  288. typedef xmutex_rec *xmutex_t;
  289. #ifndef xcondition_malloc
  290. #define xcondition_malloc() (xcondition_t)xmalloc(sizeof(xcondition_rec))
  291. #endif
  292. #ifndef xcondition_free
  293. #define xcondition_free(c) xfree((char *)c)
  294. #endif
  295. #ifndef xmutex_malloc
  296. #define xmutex_malloc() (xmutex_t)xmalloc(sizeof(xmutex_rec))
  297. #endif
  298. #ifndef xmutex_free
  299. #define xmutex_free(m) xfree((char *)m)
  300. #endif
  301. #ifndef xthread_have_id
  302. #define xthread_have_id(id) id
  303. #endif
  304. #ifndef xthread_clear_id
  305. #define xthread_clear_id(id) id = 0
  306. #endif
  307. #ifndef xthread_equal
  308. #define xthread_equal(id1,id2) ((id1) == (id2))
  309. #endif
  310. /* aids understood by some debuggers */
  311. #ifndef xthread_set_name
  312. #define xthread_set_name(t,str)
  313. #endif
  314. #ifndef xmutex_set_name
  315. #define xmutex_set_name(m,str)
  316. #endif
  317. #ifndef xcondition_set_name
  318. #define xcondition_set_name(cv,str)
  319. #endif
  320.  
  321. #endif /* _XTHREADS_H_ */
  322.